package xyh.ContributionSystem;

import xyh.ContributionSystem.ContributionSearch.*;
import xyh.ContributionSystem.ContributionSearch.SearchContribution;
import xyh.ContributionSystem.ContributionSearch.SearchMember;
import xyh.ContributionSystem.ProjectContribution.*;
import xyh.ContributionSystem.ProjectContribution.ProjectContribution;
import xyh.ContributionSystem.ProjectContribution.ProjectContributionDAO;
import xyh.ContributionSystem.ProjectMember.ProjectMember;
import xyh.ContributionSystem.ProjectMember.ProjectMemberDAO;
import xyh.ContributionSystem.ProjectMember.ProjectMemberDAOImpl;
import xyh.ContributionSystem.Projects.Project;
import xyh.ContributionSystem.Projects.ProjectDAO;
import xyh.ContributionSystem.Projects.ProjectDAOImpl;

import java.util.ArrayList;
import java.util.List;

public class ContributionSystem {
    private ProjectContributionDAO projectContributionDAO;
    private ProjectMemberDAO projectMemberDAO;
    private ProjectDAO projectDAO;

    public ContributionSystem(){
        this.projectDAO = new ProjectDAOImpl();
        this.projectMemberDAO = new ProjectMemberDAOImpl();
        this.projectContributionDAO = new ProjectContributionDAOImpl();
    }

    //根据项目ID获取项目
    public Project getProjectInfo(int projectId){
        return projectDAO.getProject(projectId);
    }
    //获取所有科研项目
    public List<Project> getAllProjects() {
        return projectDAO.getAllProjects();
    }
    //获取所所有贡献表
    public List<ProjectContribution> getAllProjectContribution(){
        return projectContributionDAO.getAllProjectContribution();
    }
    //新增项目信息
    public boolean addProject(int p_id, String p_name, int teacher_id, String p_type, String p_time, String p_task, double p_outlay){
        Project project = new Project(p_id, p_name, teacher_id, p_type, p_time, p_task, p_outlay);
        return projectDAO.addProject(project);
    }
    //根据研究生ID查询项目成员表，得到项目编号
    public List<Integer> findProjectIdByStudentId(int studentId) {
        SearchMember searchProjectMember = new SearchMember();
        searchProjectMember.setStudent_id(studentId);
        //searchProjectMember.setPm_status(1);

        List<Integer> projectIds = new ArrayList<>();
        List<ProjectMember> projectMembers = projectMemberDAO.findProjectMember(searchProjectMember);
        for (ProjectMember projectMember : projectMembers) {
            projectIds.add(projectMember.getP_id());
        }
        return projectIds;
    }
    //根据研究生ID查询项目成员表，得到通过项目编号表
    public List<Integer> findPassProjectIdByStudentId(int studentId) {
        SearchMember searchProjectMember = new SearchMember();
        searchProjectMember.setStudent_id(studentId);
        searchProjectMember.setPm_status(1);

        List<Integer> projectIds = new ArrayList<>();
        List<ProjectMember> projectMembers = projectMemberDAO.findProjectMember(searchProjectMember);
        for (ProjectMember projectMember : projectMembers) {
            projectIds.add(projectMember.getP_id());
        }
        return projectIds;
    }
    //新增项目成员表
    public boolean addProjectMember(int studentId, int projectId, int status) {
        ProjectMember projectMember = new ProjectMember();
        projectMember.setStudent_id(studentId);
        projectMember.setP_id(projectId);
        projectMember.setPm_status(status);
        return projectMemberDAO.addProjectMember(projectMember);
    }
    //根据项目id与学生id查询项目成员表
    public ProjectMember getPreojctMemberBySidAndPid(int studentId, int projectId) {
        ProjectMember projectMember = new ProjectMember();
        projectMember.setStudent_id(studentId);
        projectMember.setP_id(projectId);
        return projectMemberDAO.getProjectMember(projectMember);
    }
    //根据导师ID查询项目表
    public List<Project> getProjectsByTeacherId(int teacherId) {
        SearchProject searchProject = new SearchProject();
        searchProject.setTeacher_id(teacherId);
        List<Project> projects = projectDAO.findProject(searchProject);
        return projects;
    }
    //根据项目id与学生id修改项目成员表认证状态
    public boolean updateProjectMemberStatus(int projectId, int studentId, int status) {
        ProjectMemberDAO projectMemberDAO = new ProjectMemberDAOImpl();
        ProjectMember projectMember = getPreojctMemberBySidAndPid(studentId, projectId);
        if (projectMember == null) {
            return false;
        }
        projectMember.setPm_status(status);
        return projectMemberDAO.updateProjectMember(projectMember);
    }
    //根据项目ID修改参加经费
    public boolean updateProjectOutlayById(int p_id, double p_outlay) {
        ProjectDAO projectDAO = new ProjectDAOImpl();
        Project project = projectDAO.getProject(p_id);
        if (project == null) {
            return false;
        }
        project.setP_outlay(p_outlay);
        return projectDAO.updateProject(project);
    }
    //学生ID与项目ID新增项目贡献表
    public boolean addProjectContribution(int conID,int studentId, int projectId, String time,String type,String task, double outlay) {
        ProjectContribution projectContribution = new ProjectContribution();
        projectContribution.setContribution_id(conID);
        projectContribution.setP_type(type);
        projectContribution.setStudent_id(studentId);
        projectContribution.setP_time(time);
        projectContribution.setP_id(projectId);
        projectContribution.setP_task(task);
        projectContribution.setContribution_outlay(outlay);
        return projectContributionDAO.addProjectContribution(projectContribution);
    }
    //根据学生id查询贡献表
    public List<ProjectContribution> findContributionByStudentId(int studentId) {
        SearchContribution searchContribution = new SearchContribution();
        searchContribution.setStudent_id(studentId);
        return projectContributionDAO.findProjectContribution(searchContribution);
    }
    //根据学生id与项目id与贡献表id与信息修改贡献表
    public boolean updateContribution(int conID,int studentId, int projectId, String time,String type,String task, double outlay)  {
        ProjectContribution projectContribution = new ProjectContribution();
        projectContribution.setContribution_id(conID);
        projectContribution.setP_type(type);
        projectContribution.setStudent_id(studentId);
        projectContribution.setP_time(time);
        projectContribution.setP_id(projectId);
        projectContribution.setP_task(task);
        projectContribution.setContribution_outlay(outlay);
        return projectContributionDAO.updateProjectContribution(projectContribution);
        /*
        List<ProjectContribution> contributions = findContributionByStudentIdAndProjectId(studentId, projectId);

        ProjectContribution projectContribution = new ProjectContribution();
        projectContribution.setContribution_id(conID);
        projectContribution.setP_type(type);
        projectContribution.setStudent_id(studentId);
        projectContribution.setP_time(time);
        projectContribution.setP_id(projectId);
        projectContribution.setP_task(task);
        projectContribution.setContribution_outlay(outlay);

        for(ProjectContribution contribution : contributions) {
            contribution.setContribution_id(conID);
            contribution.setP_type(type);
            contribution.setStudent_id(studentId);
            contribution.setP_time(time);
            contribution.setP_id(projectId);
            contribution.setP_task(task);
            contribution.setContribution_outlay(outlay);
            projectContributionDAO.updateProjectContribution(contribution);
        }
        */
    }
    //根据学生id与项目id查询贡献表
    public List<ProjectContribution> findContributionByStudentIdAndProjectId(int studentId, int projectId) {
        List<ProjectContribution> contributions = new ArrayList<>();
        for(ProjectContribution contribution:projectContributionDAO.getAllProjectContribution()) {
            if (contribution.getStudent_id() == studentId && contribution.getP_id() == projectId) {
                contributions.add(contribution);
            }
        }
        return contributions;
    }
    //task
    //根据导师ID查询导师所有
    //获取审核后所有科研项目信息
    //录入项目信息
    //研究生申请科研项目
    //查询该研究生项目贡献
    //导师添加修改学生参与项目经费
    //导师查询学生id，根据id查询项目贡献表，修改学生经费

}
